package com.herui.sdyu_lib.aliyun.oss;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.herui.sdyu_lib.base.BaseAppcation;


/**
 * Created by amanda.shan on 2016/7/20.
 */
public class AliOssApp {

    private static AliOssApp instance;

    public static AliOssApp getInstance() {

        if (instance == null) {
            synchronized (AliOssApp.class) {
                if (instance == null) {
                    instance = new AliOssApp();
                }
            }
        }
        return instance;
    }

    private BaseAppcation appcation;
    private OSS oss;
    private AliOssUtils aliOssUtils;

    public void init(BaseAppcation appcation) {
        this.appcation = appcation;
        aliOssUtils = new AliOssUtils();
    }

    public void testInit(){
        aliOssUtils = new AliOssUtils();
    }

    public BaseAppcation getAppcation() {
        return appcation;
    }

    public AliOssUtils getAliOssUtils() {
        return aliOssUtils;
    }

    public OSS getOss() {
        return oss;
    }

    public void setOss(OSS oss) {
        this.oss = oss;
    }

    public void initOSSClient(String accessKeyId, String secretKeyId, String securityToken) {
        // 明文设置secret的方式建议只在测试时使用，更多鉴权模式请参考后面的访问控制章节
        OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider
                (accessKeyId, secretKeyId, securityToken);

        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

        oss = new OSSClient(appcation, AliOssConfig.endpoint, credentialProvider,
                conf);
    }
}
